home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / zm16src.lzh / SZ.C < prev    next >
C/C++ Source or Header  |  1988-05-19  |  32KB  |  1,543 lines

  1. /*
  2.  *                ACKNOWLEDGEMENTS
  3.  *
  4.  *    ZMDM was derived from rz/sz for Unix  posted by 
  5.  *    Chuck Forsberg (...!tektronix!reed!omen!caf ). We
  6.  *    thank him for his excellent code, and for giving
  7.  *    us permission to use and distribute his code and
  8.  *    documentation.
  9.  *
  10.  *    Atari St version by:
  11.  *        Jwahar Bammi
  12.  *            usenet: mandrill!bammi@{decvax,sun}.UUCP
  13.  *            csnet:  bammi@mandrill.ces.CWRU.edu
  14.  *            arpa:   bammi@mandrill.ces.CWRU.edu
  15.  *            CompuServe: 71515,155
  16.  */
  17.  
  18. #include "config.h"
  19. #define SVERSION "sz 1.23 01-15-87"
  20. #define SSTVERSION "sz 1.01 03-07-87"
  21. #define OS    "Unix V7/BSD"
  22.  
  23. #ifndef STANDALONE
  24. #define RETURN return
  25. #else
  26. int bibi() {} /* dummy */
  27. #endif 
  28.  
  29. /* #define SDEBUG */
  30.  
  31. /*
  32.  * sz.c By Chuck Forsberg
  33.  *
  34.  *    cc -O sz.c -o sz        USG (SYS III/V) Unix
  35.  *     cc -O -DV7  sz.c -o sz        Unix Version 7, 2.8 - 4.3 BSD
  36.  *
  37.  *        define CRCTABLE to use table driven CRC
  38.  *
  39.  *  ******* Some systems (Venix, Coherent, Regulus) do not *******
  40.  *  ******* support tty raw mode read(2) identically to    *******
  41.  *  ******* Unix. ONEREAD must be defined to force one     *******
  42.  *  ******* character reads for these systems.           *******
  43.  *
  44.  * A program for Unix to send files and commands to computers running
  45.  *  Professional-YAM, PowerCom, YAM, IMP, or programs supporting Y/XMODEM.
  46.  *
  47.  *  Sz uses buffered I/O to greatly reduce CPU time compared to UMODEM.
  48.  *
  49.  *  USG UNIX (3.0) ioctl conventions courtesy Jeff Martin
  50.  *
  51.  *    St v 1.01
  52.  *        added support for 32 bit CRC's (Zmodem) ++jrb
  53.  *
  54.  */
  55.  
  56. #include "zmdm.h"
  57. #include "common.h"
  58. #include "zmodem.h"
  59.  
  60. #ifndef Vsync             /* Atari forgot these in osbind.h */
  61. #define Vsync()    xbios(37)
  62. #endif
  63.  
  64. #ifndef Supexec
  65.         /* Some versions of osbind don't define Supexec */
  66. #define Supexec(X) xbios(38,X)
  67. #endif
  68.  
  69. #define SLOGFILE "szlog"
  70.  
  71. #define purgeline()    while(Bconstat(1)) Bconin(1)
  72. #define S_IFDIR 0x0010
  73.  
  74. /*
  75.  * Attention string to be executed by receiver to interrupt streaming data
  76.  *  when an error is detected.  A pause (0336) may be needed before the
  77.  *  ^C (03) or after it.
  78.  */
  79. #ifdef READCHECK
  80. char Myattn[] = { 0 };
  81. #else
  82. #ifdef USG
  83. char Myattn[] = { 03, 0336, 0 };
  84. #else
  85. char Myattn[] = { 0 };
  86. #endif
  87. #endif
  88.  
  89. #if (MWC || MANX)
  90. FILE *fopen();
  91. #else
  92. FILE *fopen(), *fopenb();
  93. #endif
  94. static unsigned long SaveIntr;
  95. static int Resuming, ForceBin;
  96. static int in;
  97.  
  98. /* called by signal interrupt or terminate to clean things up */
  99. bibis(n)
  100. int n;
  101. {
  102.     canit(); flush_modem(); mode(0);
  103.     fprintf(STDERR, "\r\nsz: caught signal %d; exiting\n", n);
  104.  
  105.     aexit(128+n);
  106. }
  107.  
  108. /* Called when Zmodem gets an interrupt (^X) */
  109. #ifdef ONINTR
  110. onintr()
  111. {
  112.     siggi = 0;
  113.     longjmp(intrjmp, -1);
  114. }
  115. #endif
  116.  
  117. #define ZKER
  118. int Zctlesc;    /* Encode control characters */
  119.  
  120. #ifdef STANDALONE
  121. int main(argc, argv)
  122. #else
  123. int dosz(argc, argv)
  124. #endif
  125. int argc;
  126. char **argv;
  127. {
  128.     register char *cp;
  129.     register int npats;
  130.     int agcnt; char **agcv;
  131.     char **patts;
  132.  
  133. #ifdef STANDALONE
  134. #if (MWC || MANX)
  135.     extern char *lmalloc();
  136. #endif
  137.  
  138.     /* Set up Dta */
  139.     Fsetdta(&statbuf);
  140.  
  141.     /* Get screen rez */
  142.     rez = Getrez();
  143.     drv_map = Drvmap();
  144.  
  145. #if (MWC || MANX)
  146. #ifndef DYNABUF
  147.     if((bufr = (unsigned char *)lmalloc((unsigned long)BBUFSIZ))
  148.                      == (unsigned char *)NULL)
  149. #else
  150.     if((bufr = dalloc()) == (unsigned char *)NULL)
  151. #endif /* DYNABUF */
  152.     {
  153. #ifdef REMOTE
  154.         Bauxws("Sorry, could not allocate enough memory\r\n");
  155. #else
  156.         Bconws("Sorry, could not allocate enough memory\r\n");
  157. #endif
  158.  
  159.         Pterm(4);
  160.     }
  161. #else /* MWC || MANX */
  162. #ifdef DYNABUF
  163.     if((bufr = dalloc()) == (unsigned char *)NULL)
  164.     {
  165. #ifdef REMOTE
  166.         Bauxws("Sorry, could not allocate enough memory\r\n");
  167. #else
  168.         Bconws("Sorry, could not allocate enough memory\r\n");
  169. #endif
  170.         Pterm(5);
  171.     }
  172. #endif /* DYNABUF */
  173. #endif /* MWC || MANX */
  174.  
  175. #ifndef REMOTE
  176.     STDERR = stderr;
  177. #else
  178. #ifndef DLIBS
  179.     if((STDERR = fopen("aux:", "rw")) == (FILE *)NULL)
  180.     {
  181.         Bauxws("Could not Open Aux Stream for Stderr\r\n");
  182.         finish();
  183.     }
  184.     setbuf(STDERR, (char *)NULL);
  185. #else
  186.     STDERR = stdaux;
  187. #endif /* DLIBS */
  188.     
  189. #endif /* REMOTE */
  190.     {
  191.         int speed;
  192.         speed = getbaud();
  193.         Baudrate = BAUD_RATE(speed);
  194.         SetIoBuf();
  195.         Rsconf(speed, 0,-1,-1,-1,-1);
  196.         Vsync(); Vsync();
  197.     }
  198. #endif /* STANDALONE */
  199.  
  200.     SendType = 1;
  201.     Rxtimeout = 600;
  202.     npats=0;
  203.     if (argc<2)
  204.     {
  205.         susage();
  206.         RETURN(1);
  207.     }
  208.  
  209.     initz();
  210. #ifndef STANDALONE
  211.     schkinvok(argv[0]);
  212. #else
  213.     Progname = "sz";
  214. #endif
  215.  
  216.     SaveIntr = Setexc(0x0102, -1L);
  217.     BusErr   = Setexc(2, -1L);
  218.     AddrErr  = Setexc(3, -1L);
  219.  
  220.     Verbose = 0;
  221.     Resuming = FALSE;
  222.     ForceBin = FALSE;
  223.     in = (-1);
  224.     vdebug = 0;
  225.  
  226. #ifdef SDEBUG
  227.     logf = (FILE *)NULL;
  228. #endif
  229.     while (--argc) {
  230.         cp = *++argv;
  231.         if (*cp++ == '-' && *cp) {
  232.             while ( *cp) {
  233.                 switch(*cp++) {
  234.                 case '+':
  235.                     Lzmanag = ZMAPND; break;
  236. #ifdef CSTOPB
  237.                 case '2':
  238.                     Twostop = TRUE; break;
  239. #endif
  240.                 case '7':
  241.                     Wcsmask=0177; break;
  242.  
  243. /*
  244.     On the St we look up the ext and decide. For Xmodem
  245.     transfers, the file is always sent in binary mode
  246.     and it is the responsibility of the receiver to
  247.     strip CR if so desired.
  248.                 case 'a':
  249.                     Lzconv = ZCNL;
  250.                     Ascii = TRUE; break;
  251.                 case 'b':
  252.                     Lzconv = ZCBIN; break;
  253. */
  254.  
  255. /*  ST extention, force binary, useful to back up every thing
  256.  *  in image mode, see -B option of rz too +jrb
  257.  */
  258.                 case 'B':
  259.                     ForceBin = TRUE;
  260.                     Lzconv = ZCBIN;
  261.                     break;
  262.                 case 'C':
  263.                     if (--argc < 1) {
  264.                         susage();
  265.                         RETURN(1);
  266.                     }
  267.                     Cmdtries = atoi(*++argv);
  268.                     break;
  269.                 case 'i':
  270.                     Cmdack1 = ZCACK1;
  271.                     /* **** FALL THROUGH TO **** */
  272.                 case 'c':
  273.                     if (--argc != 1) {
  274.                         susage();
  275.                         RETURN(1);
  276.                     }
  277.                     Command = TRUE;
  278.                     Cmdstr = *++argv;
  279.                     break;
  280.                 case 'd':
  281.                     ++Dottoslash;
  282.                     /* **** FALL THROUGH TO **** */
  283.                 case 'f':
  284.                     Fullname=TRUE; break;
  285.                 case 'E':
  286.                     Zctlesc = (-1); break;
  287.                 case 'e':
  288.                     Zctlesc = 1; break;
  289.                 case 'k':
  290.                     Blklen=KSIZE; break;
  291.                 case 'L':
  292.                     if (--argc < 1) {
  293.                         susage();
  294.                         RETURN(1);
  295.                     }
  296.                     blkopt = atoi(*++argv);
  297.                     if (blkopt<32 || blkopt>1024)
  298.                     {
  299.                         susage();
  300.                         RETURN(1);
  301.                     }
  302.                     break;
  303.                 case 'l':
  304.                     if (--argc < 1) {
  305.                         susage();
  306.                         RETURN(1);
  307.                     }
  308.                     Tframlen = atoi(*++argv);
  309.                     if (Tframlen<32 || Tframlen>1024)
  310.                     {
  311.                         susage();
  312.                         RETURN(1);
  313.                     }
  314.                     break;
  315.                 case 'N':
  316.                     Lzmanag = ZMDIFF;  break;
  317.                 case 'n':
  318.                     Lzmanag = ZMNEW;  break;
  319.                 case 'o':
  320.                     Wantfcs32 = FALSE; break;
  321.                 case 'p':
  322.                     Lzmanag = ZMPROT;  break;
  323.                 case 'r':
  324.                     Lzconv = ZCRESUM; Resuming = TRUE; break;
  325.                 case 'q':
  326.                     Quiet=TRUE; Verbose=0; break;
  327.                 case 't':
  328.                     if (--argc < 1) {
  329.                         susage();
  330.                         RETURN(1);
  331.                     }
  332.                     Rxtimeout = atoi(*++argv);
  333.                     if (Rxtimeout<10 || Rxtimeout>1000)
  334.                     {
  335.                         susage();
  336.                         RETURN(1);
  337.                     }
  338.                     break;
  339. #ifdef TESTATTN
  340.                 case 'T':
  341.                     Testattn = TRUE; break;
  342. #endif
  343.                 case 'u':
  344.                     ++Unlinkafter; break;
  345.                 case 'v':
  346.                     ++Verbose; break;
  347.                 case 'X':
  348.                     ++Modem; break;
  349.                 case 'y':
  350.                     Lzmanag = ZMCLOB; break;
  351.                 default:
  352.                     susage();
  353.                     RETURN(1);
  354.                 }
  355.             }
  356.         }
  357.         else if ( !npats && argc>0) {
  358.             if (argv[0][0]) {
  359.                 npats=argc;
  360.                 patts=argv;
  361.             }
  362.         }
  363.     }
  364.     if (npats < 1 && !Command) 
  365.     {
  366.         susage();
  367.         RETURN(1);
  368.     }
  369.  
  370. #ifdef SDEBUG
  371.     if (Verbose > 2)
  372.     {
  373.         if ((logf = fopen(SLOGFILE, "a"))== (FILE *)NULL)
  374.         {
  375.             fprintf(STDERR, "Can't open log file %s\n",SLOGFILE);
  376.             RETURN(0200);
  377.         }
  378.         fprintf(logf, "Progname=%s\n", Progname);
  379.         vdebug = 1;
  380.         fflush(logf);
  381.     }
  382. #endif
  383.  
  384.     if ( !Quiet)
  385.     {
  386.         if (Verbose < 2)
  387.             Verbose = 2;
  388.     }
  389.  
  390.  
  391.     Setexc(0x0102, bibis);
  392.     Setexc(2, buserr);
  393.     Setexc(3, addrerr);
  394.  
  395.     if(setjmp(busjmp))
  396.     {
  397.         /* On a bus error - instead of 2 bombs */
  398.         fprintf(STDERR,"\r\nFATAL: Bus Error\n\n");
  399. #ifdef SDEBUG
  400.         if(logf != (FILE *)NULL)
  401.             fclose(logf);
  402. #endif
  403.         if(in != -1)
  404.         {
  405.             stfclose(in);
  406.             in = (-1);
  407.         }
  408.         canit();
  409.         Setexc(2, BusErr);
  410.         Setexc(3, AddrErr);